package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口（管理端）")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐接口
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐接口")
    //由于新增套餐只会影响对应类别的缓存数据，且套餐id容易获得，所以选择精确清理
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售，停售接口
     * @return
     */
    @PostMapping("status/{status}")
    @ApiOperation("套餐起售/停售")
    //由于获得套餐id对应的类别id需要查询数据库，精确清理得不偿失，所以直接全清
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable("status") Integer status,@RequestParam Long id) {
        setmealService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 修改套餐以及与菜品对应关系的接口
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐以及该套餐与菜品的对应关系")
    //由于修改的可能是套餐的类别，受影响的缓存条数大于1，所以选择直接全清
    @CacheEvict(cacheNames = "setmealVOCache",allEntries = true)//删除该套餐id对应的套餐缓存数据
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询套餐数据
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    //如果要将query类型请求参数封装进入对象，就不能加@RequestParam注解
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询：{}",setmealPageQueryDTO);
        PageResult data=setmealService.page(setmealPageQueryDTO);
        return Result.success(data);
    }

    /**
     * 根据id查询相关套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询相关套餐接口")
    public Result<SetmealVO> searchById(@PathVariable Long id) {
        SetmealVO data=setmealService.searchSetmealById(id);
        return Result.success(data);
    }

    /**
     * 批量删除套餐
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    //由于查询ids对应的所有套餐类别代价过大，进行sql查询后清理得不偿失，选择直接全清
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("批量删除套餐:{}",ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }
}
